package com.zzy.mccjt.gateway.common.enums;

import lombok.Getter;

/**
 * @author Huangzq
 * @title: GateWayPreEnum
 * @projectName zhengtyun-saas
 * @date 2019/5/5 15:06
 */
@Getter
public enum GateWayPreEnum {

    /**
     * 系统
     */
    CUSTOMER("system"),
    /**
     * 客户
     */
    MERCHANT("customer");

    private String prefix;

    GateWayPreEnum(String prefix){
        this.prefix=prefix;
    }
    /**
     * 获取有效token
     * param  type
     * @param token
     * @return Boolean
     */
    public static String getToken(GateWayPreEnum  gateWayType,String token) {
        /*if (StringUtils.isBlank(token)) {
            return null;
        }*/
        int preFixLen=gateWayType.getPrefix().length();
        if (token.length()<preFixLen) {
            return null;
        }
        // 截取token前缀
        String prefix = token.substring(0, preFixLen);
        if(!prefix.equals(gateWayType.getPrefix())){
            return null;
        }
        return token.substring(preFixLen+1);
    }

    /**
     * 根据token获取 token类型
     *
     * @param token
     * @return
     */
    public static GateWayPreEnum get(String token) {
        /*if (StringUtils.isBlank(token)) {
            return null;
        }*/
        if (!token.contains(" ")) {
            return null;
        }

        // 截取token前缀
        String prefix = token.substring(0, token.indexOf(" "));

        /*if (StringUtils.isBlank(prefix)) {
            return null;
        }*/

        for (GateWayPreEnum value : GateWayPreEnum.values()) {
            if (value.getPrefix().equalsIgnoreCase(prefix)) {
                return value;
            }
        }
        return null;
    }
}
